package com.ztesoft.sca.dao.user;

import com.ztesoft.sca.model.user.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liang.aozhao
 * @created 2018/5/15 21:54
 */
@Repository
public class ImUserDaoImpl implements ImUserDao {
    private static final Logger logger = LoggerFactory.getLogger(ImUserDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public long addImUser(ImUserPo imUserPo) throws Exception {
        String sql = "INSERT INTO IM_USER(USER_ID, USER_NAME,NICK_NAME, PASSWD,SIGN, AVATAR,MOBILE_TEL, HOME_TEL, EMAIL, REGISTER_TIME) VALUES(?,?,?,?,?,?,?,?,?,SYSDATE)";
        this.jdbcTemplate.update(sql, new Object[]{imUserPo.getUserId(), imUserPo.getUserName(), imUserPo.getNickName(), imUserPo.getPasswd(), imUserPo.getSign(), imUserPo.getAvatar(), imUserPo.getMobileTel(), imUserPo.getHomeTel(), imUserPo.getEmail()});
        return imUserPo.getUserId();
    }

    @Override
    public void deleteImUser(ImUserPo imUserPo) throws Exception {
        String sql = "DELETE FROM IM_USER WHERE USER_ID = ?";
        this.jdbcTemplate.update(sql, imUserPo.getUserId());
    }

    @Override
    public void updateImUser(ImUserPo imUserPo) throws Exception {
        StringBuffer sql = new StringBuffer("UPDATE IM_USER SET ");
        List<Object> objList = new ArrayList<>();
        if (imUserPo.getUserName() != null && !"".equals(imUserPo.getUserName()) && !"null".equals(imUserPo.getUserName())) {
            sql.append(" USER_NAME = ? ,");
            objList.add(imUserPo.getUserName());
        }
        if (imUserPo.getNickName() != null && !"".equals(imUserPo.getNickName()) && !"null".equals(imUserPo.getNickName())) {
            sql.append(" NICK_NAME = ? ,");
            objList.add(imUserPo.getNickName());
        }
        if (imUserPo.getPasswd() != null && !"".equals(imUserPo.getPasswd()) && !"null".equals(imUserPo.getPasswd())) {
            sql.append(" PASSWD = ? ,");
            objList.add(imUserPo.getPasswd());
        }
        if (imUserPo.getSign() != null && !"".equals(imUserPo.getSign()) && !"null".equals(imUserPo.getSign())) {
            sql.append(" SIGN = ? ,");
            objList.add(imUserPo.getSign());
        }
        if (imUserPo.getAvatar() != null && !"".equals(imUserPo.getAvatar()) && !"null".equals(imUserPo.getAvatar())) {
            sql.append(" AVATAR = ? ,");
            objList.add(imUserPo.getAvatar());
        }
        if (imUserPo.getMobileTel() != null && !"".equals(imUserPo.getMobileTel()) && !"null".equals(imUserPo.getMobileTel())) {
            sql.append(" MOBILE_TEL = ? ,");
            objList.add(imUserPo.getMobileTel());
        }
        if (imUserPo.getHomeTel() != null && !"".equals(imUserPo.getHomeTel()) && !"null".equals(imUserPo.getHomeTel())) {
            sql.append("HOME_TEL = ?,");
            objList.add(imUserPo.getHomeTel());
        }
        if (imUserPo.getEmail() != null && !"".equals(imUserPo.getEmail()) && !"null".equals(imUserPo.getEmail())) {
            sql.append(" EMAIL = ? ,");
            objList.add(imUserPo.getEmail());
        }
        sql  = new StringBuffer(sql.toString().trim());
        sql.deleteCharAt(sql.length() - 1);
        sql.append(" where USER_ID= ? ");
        objList.add(imUserPo.getUserId());
        //jdbcTemplate只接受数组参数,转换一下
        Object[] args = objList.toArray();
        this.jdbcTemplate.update(sql.toString(), args);
    }

    @Override
    public ImUserPo getImUser(ImUserPo imUserPo) throws Exception {
        ImUserPo imUserResult;
        String sql = "SELECT USER_ID,PASSWD,USER_NAME,NICK_NAME,SIGN,AVATAR,MOBILE_TEL,HOME_TEL,EMAIL,REGISTER_TIME FROM IM_USER WHERE USER_ID = ?";
        imUserResult = this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(ImUserPo.class), imUserPo.getUserId());
        return imUserResult;
    }

    @Override
    public List<ImUserPo> listImUsersByRoleId(Long roleId) throws Exception {
        String sql = "SELECT A.USER_ID,A.PASSWD,A.USER_NAME,A.NICK_NAME,A.SIGN,A.AVATAR,A.MOBILE_TEL,A.HOME_TEL,A.EMAIL,A.REGISTER_TIME FROM IM_USER A ,IM_ROLE_USER_RELA B WHERE B.ROLE_ID = ? AND A.USER_ID = B.USER_ID ";
        List<ImUserPo> imUserPoList = this.jdbcTemplate.query(sql, new Object[]{roleId}, BeanPropertyRowMapper.newInstance(ImUserPo.class));
        return imUserPoList;
    }

    @Override
    public List<ImUserPo> listImUserByOrgId(Long orgId) throws Exception {
        String sql = "SELECT A.USER_ID,A.PASSWD,A.USER_NAME,A.NICK_NAME,A.SIGN,A.AVATAR,A.MOBILE_TEL,A.HOME_TEL,A.EMAIL,A.REGISTER_TIME FROM IM_USER A ,IM_ROLE_USER_RELA B , IM_ORG_ROLE_RELA C WHERE C.ORG_ID = ? AND B.ROLE_ID = C.ROLE_ID AND A.USER_ID = B.USER_ID";
        List<ImUserPo> imUserPoList = this.jdbcTemplate.query(sql, new Object[]{orgId}, BeanPropertyRowMapper.newInstance(ImUserPo.class));
        return imUserPoList;
    }

    @Override
    public void addImUserRoleRela(Long userId, Long roleId) throws Exception {
        String sql = "INSERT INTO IM_ROLE_USER_RELA (ROLE_ID, USER_ID) VALUES (? , ?)";
        this.jdbcTemplate.update(sql, roleId, userId);
    }

    @Override
    public void deleteImUserRoleRela(Long userId, Long roleId) throws Exception {
        String sql = "DELETE FROM IM_ROLE_USER_RELA WHERE ROLE_ID = ? AND USER_ID = ?";
        this.jdbcTemplate.update(sql, roleId, userId);
    }

    @Override
    public List<ImPrivilegePo> listImPrivilegeByUserId(Long userId) throws Exception {
        String sql = "SELECT A.PRIVILEGE_ID , A.BUTTON_ID ,A.MENU_ID ,A.ROLE_ID , A.ACCESS_TYPE FROM IM_PRIVILEGE A , IM_ROLE_USER_RELA B WHERE B.USER_ID = ? AND A.ROLE_ID = B.ROLE_ID ";
        List<ImPrivilegePo> imPrivilegePoList = this.jdbcTemplate.query(sql,new Object[]{userId},BeanPropertyRowMapper.newInstance(ImPrivilegePo.class));
        return imPrivilegePoList;
    }

}
